package com.jyxd.web.service.czform;

import com.jyxd.web.dao.czform.ECMOOutputDao;
import com.jyxd.web.dao.czform.ECMORecordDao;
import com.jyxd.web.data.czform.ECMOOutput;
import com.jyxd.web.data.czform.ECMORecord;
import com.jyxd.web.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ECMORecordService {

    @Autowired
    private ECMORecordDao ecmoRecordDao;
    @Autowired
    private ECMOOutputDao ecmoOutputDao;

    public boolean insert (ECMORecord ecmoRecord) {
        ecmoRecord.setStatus(1);
        boolean flag = ecmoRecordDao.insert(ecmoRecord);
        if (flag) {
            List<ECMOOutput> ecmoOutputList = ecmoRecord.getEcmoOutputList();
            if (ecmoOutputList.size() > 0) {
                for (int i = 0; i < ecmoOutputList.size(); i++) {
                    ECMOOutput ecmoOutput = ecmoOutputList.get(i);
                    ecmoOutput.setId(UUIDUtil.getUUID());
                    ecmoOutput.setStatus(1);
                    ecmoOutput.setRecordTd(ecmoRecord.getId());
                    flag = ecmoOutputDao.insert(ecmoOutput);
                }
            }
        }
        return flag;
    }

    public boolean update (ECMORecord ecmoRecord) {
        return ecmoRecordDao.update(ecmoRecord);
    }

    public ECMORecord queryData (String id) {
        ECMORecord ecmoRecord = ecmoRecordDao.queryData(id);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("recordTd",id);
        List<ECMOOutput> ecmoOutputs = ecmoOutputDao.queryList(map);
        ecmoRecord.setEcmoOutputList(ecmoOutputs);
        return ecmoRecord;
    }

    public List<ECMORecord> queryList (Map<String, Object> map) {
        List<ECMORecord> ecmoRecordsList = ecmoRecordDao.queryList(map);
        if (ecmoRecordsList.size()>0){
            for (int i=0;i<ecmoRecordsList.size();i++){
                ECMORecord ecmoRecord = ecmoRecordsList.get(i);
                Map<String,Object> selectMap = new HashMap<String,Object>();
                selectMap.put("recordTd",ecmoRecord.getId());
                List<ECMOOutput> ecmoOutputs = ecmoOutputDao.queryList(selectMap);
                ecmoRecord.setEcmoOutputList(ecmoOutputs);
            }
        }
        return ecmoRecordsList;
    }

    public int queryNum (Map<String, Object> map) {
        return ecmoRecordDao.queryNum(map);
    }
}
